<template>
  <div class="flex">
    <section>
      <div class="flex-row">
        <div class="col-md-6">
          <checker-left v-ref:left></checker-left>
        </div>
        <div class="btn-group-vertical col-md">
          <button class="btn btn-success width-80" @click="confirm()" >保存</button>
          <button class="btn btn-success width-80" @click="addRight()" >&gt;</button>
          <button class="btn btn-success width-80" @click="addLeft()" >&lt;</button>
        </div>
        <div class="col-md-6">
          <checker-right v-ref:right></checker-right>
        </div>
      </div>
    </section>
  </div>
</template>

<script>
import CheckerLeft from './CheckerLeft'
import CheckerRight from './CheckerRight'
import co from 'co'

// 保存更新内容
let saveGen = function * (self) {
  self.model.plan_one_id = self.f_plan_id_a
  self.model.plan_two_id = self.f_plan_id_b
  self.model.checker_one_id = self.$refs.left.model.chosenOrg.id
  self.model.checker_two_id = self.$refs.right.model.chosenOrg.id
  self.model.checkerA = []
  for(let i=0; i< self.$refs.left.model.rows.length; i++) {
    let row = self.$refs.left.model.rows[i]
    if(row.sign == '1')
      self.model.checkerA.push(row)
  }
  self.model.checkerB = []
  for(let i=0; i< self.$refs.right.model.rows.length; i++) {
    let row = self.$refs.right.model.rows[i]
    if(row.sign == '1')
      self.model.checkerB.push(row)
  }
  yield self.$post('rs/logic/计划调整', {data: self.model})
  self.$refs.left.$refs.paged.$refs.criteria.search()
  self.$refs.right.$refs.paged.$refs.criteria.search()
}

export default {
  title: '安检计划调整',
  props: ['f'],
  data () {
    return {
      model: {},
      leftInfor: {},
      rightInfor: {},
      f_plan_id_a: '',
      f_plan_id_b: ''
    }
  },
  ready () {
    this.$refs.right.$refs.paged.$refs.criteria.model.f_plan_year = this.$refs.left.model.c_plan_year[0]
    this.$refs.right.$refs.paged.$refs.criteria.model.f_plan_month = this.$refs.left.model.c_plan_month[0]
  },
  methods: {
    // 向右边调整
    addRight () {
      if (this.$refs.left.selected !== null) {
        // 为调整的数据添加标记
        if (this.$refs.left.selected.sign === '1') {
          this.$refs.left.selected.sign = null
        } else {
          this.$refs.left.selected.sign = '1'
          this.f_plan_id_a = this.$refs.left.selected.f_plan_id
        }
        // 压入数组中
        if (this.$refs.left.selected !== this.leftInfor) {
          this.$refs.right.model.rows.push(this.$refs.left.selected)
          this.$refs.left.model.rows.$remove(this.$refs.left.selected)
          this.$refs.left.model.count -= 1
          this.$refs.right.model.count += 1
          this.leftInfor = this.$refs.left.selected
        }
      }
    },
    // 向左边调整
    addLeft () {
      // 为调整的数据添加标记
      if (this.$refs.right.selected !== null) {
        if (this.$refs.right.selected.sign === '1') {
          this.$refs.right.selected.sign = null
        } else {
          this.$refs.right.selected.sign = '1'
          this.f_plan_id_b = this.$refs.right.selected.f_plan_id
        }
        // 压入数组中
        if (this.$refs.right.selected !== this.rightInfor) {
          this.$refs.left.model.rows.push(this.$refs.right.selected)
          this.$refs.right.model.rows.$remove(this.$refs.right.selected)
          this.$refs.right.model.count -= 1
          this.$refs.left.model.count += 1
          this.rightInfor = this.$refs.right.selected
        }
      }
    },
    confirm () {
      if(!this.f_plan_id_a)
        this.f_plan_id_a = this.$refs.left.model.rows[0].f_plan_id
      if(!this.f_plan_id_b)
        this.f_plan_id_b = this.$refs.right.model.rows[0].f_plan_id
      if(this.f_plan_id_a == this.f_plan_id_b) {
        window.alert('调整计划不能相同！')
        return
      }
      let gen = saveGen(this)
      return co(gen)
    }
  },
  components: { CheckerLeft, CheckerRight }
}
</script>
